package com.iocup.keybastion.context;


/**
 * 安全上下人的持有者
 *
 * @author xyjxust
 * @create 2020/3/9 13:35
 **/
public class SecurityContextHolder {

    private static SecurityContextHandler securityContextHandler;
    public static final String MODE_THREADLOCAL = "threadLocal";
    private static String CONTEXT_MODEL = "threadLocal";

    public static SecurityContext getContext() {
        return securityContextHandler.get();
    }

    public static void setContext(SecurityContext securityContext) {
        securityContextHandler.set(securityContext);
    }

    public static void clearContext() {
        securityContextHandler.clear();
    }

    static {
        initialize();
    }

    /**
     * 初始化上下文
     */
    private static void initialize() {
        if (MODE_THREADLOCAL.equals(CONTEXT_MODEL)) {
            securityContextHandler = new ThreadLocalSecurityContextHandler();
        }
        if (securityContextHandler == null) {
            securityContextHandler = new ThreadLocalSecurityContextHandler();
        }
    }
}
